<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Logic\finger\MinerLogic;


class MinerController extends Controller
{
    /**
     *  添加矿机，列表，绑定用户，详情
     */


    /**
     * @OA\Post(
     *     path="/admin/minerAdd",
     *     tags={"矿机"},
     *     summary="添加",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="product_id", type="number", description="产品id product_id"),
     *                 @OA\Property(property="capacity", type="number", description="容量"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function add(Request $request)
    {
        $data = $request->all();
        return MinerLogic::add($data);

    }

    /**
     * @OA\Post(
     *     path="/admin/minerIndex",
     *     tags={"矿机"},
     *     summary="列表",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"key","val"},
     *                 @OA\Property(property="key", type="string", description="搜索字段 mid,m_number,user_name"),
     *                 @OA\Property(property="val", type="string", description="搜索值"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function index(Request $request)
    {
        $where = [];
        if($request->has('limit')){
            $limit = $request->input('limit');
        }else{
            $limit = 15;
        }
        $post_data = $request->all();
        if(isset($post_data['key']) && isset($post_data['val']) && $post_data['key'] != "" && $post_data['val'] != ""){

            if ( $post_data['key'] == 'user_name'){
                $key = 'mu.'.$post_data['key'];
            } elseif( $post_data['key'] == 'phone') {
                $key = 'u.'.$post_data['key'];
            }else{
                $key = 'mi.'.$post_data['key'];
            }
            $where[] = [$key,'=',$post_data['val']];
        }

        if(isset($this->user['id'])){
            $where[] = ['u.username','=',$this->user['username']];
        }

        $miner_list = Db::table('miner_host as mh')
            ->leftJoin('miner_info as mi','mi.mid','=','mh.mid')
            ->leftJoin('miner_user as mu','mu.mid','=','mh.mid')
            ->leftJoin('user as u','u.username','=','mu.user_name')
            ->where($where)
            ->orderBy('add_time','desc')
            ->select('mh.*','mi.m_number','mu.user_name','u.phone','mu.accessory','mu.note as mu_note')
            ->paginate($limit);

        return s_msg('',$miner_list);

    }



    /**
     * @OA\Post(
     *     path="/minerIndex",
     *     tags={"矿机"},
     *     summary="列表",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"key","val"},
     *                 @OA\Property(property="type", type="string", description="0未开始 1开始 2结束"),
     *                 @OA\Property(property="product_id", type="string", description="1算力  2矿机"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function home(Request $request)
    {
        $where = [];
        if($request->has('limit')){
            $limit = $request->input('limit');
        }else{
            $limit = 15;
        }

        $post_data = $request->all();

        if(isset($this->user['id'])){
            $where[] = ['u.username','=',$this->user['username']];
        }

        if($request->has('product_id')){
            $note = $request->input('product_id');
            $where[] = ['mi.note','=',$note];
        }


        if ($request->has('type')) {

            $time = time();
            $type = $request->input('type');
            if ($type == 0) {
                $where[] = ['mh.start_time', '>', $time];
            } elseif ($type == 1) {
                $where[] = ['mh.start_time', '<=', $time];
                $where[] = ['mh.end_time', '>=', $time];
            } else {
                $where[] = ['mh.end_time', '<', $time];
            }
        }

        $miner_list = Db::table('miner_host as mh')
            ->join('miner_info as mi', 'mh.mid', '=', 'mi.mid')
            ->join('miner_user as mu', 'mu.mid', '=', 'mh.mid')
            ->leftJoin('user as u','u.username','=','mu.user_name')
            ->where($where)
            ->select(
                'mh.id',
                'mh.hspace',
                'mh.mid',
                'mh.token',
                'mi.m_number',
                'mi.note'
            )
            ->orderByDesc('mh.id')
            ->paginate($limit);



        foreach($miner_list as $k=> $v){
            $profit = DB::table('miner_balance')->where('mid',$v->mid)->where('type','mining')->sum('amount');
            //$miner_list[$k]->profit = $profit;
            $miner_list[$k]->profit = number_format($profit , 6);//保留6位小数
        }

        return s_msg('',$miner_list);

    }

    public function test(Request $request)
    {

        $miner_list = Db::table('miner_host as mh')
            ->leftJoin('miner_balance as mb','mb.mid','=','mh.mid')
            ->select(
                'mh.id',
                DB::raw('sum(mb.amount) as amount')
            )
            ->groupBy('mh.id')
            ->get();
        dd($miner_list);
        return s_msg('',$miner_list);

    }




    /**
     * @OA\Post(
     *     path="/admin/minerBind",
     *     tags={"矿机"},
     *     summary="绑定用户",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"phone","mid"},
     *                 @OA\Property(property="phone", type="string", description="用户手机号"),
     *                 @OA\Property(property="mid", type="string", description="矿机mid"),
     *                 @OA\Property(property="accessory", type="string", description="附件"),
     *                 @OA\Property(property="note", type="string", description="备注"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function bind(Request $request)
    {
        $data = $request->all();
        return MinerLogic::bind($data);

    }



    /**
     * @OA\Post(
     *     path="/admin/minerXq",
     *     tags={"矿机"},
     *     summary="详情",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"id"},
     *                 @OA\Property(property="id", type="string", description="矿机mid,mid"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    /**
     * @OA\Post(
     *     path="/minerXq",
     *     tags={"矿机"},
     *     summary="详情",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"id"},
     *                 @OA\Property(property="id", type="string", description="矿机mid,mid"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function xq(Request $request)
    {
          if($request->has('mid')){

              $mid = $request->input('mid');

              $user_name = DB::table('miner_user')
                  ->where('mid',$mid)
                  ->value('user_name');

              if(isset($this->user['id']) && $user_name != $this->user['username']){
                  return e_msg(123456,'您不能看别人的矿机');
              }

              $xq = Db::table('miner_host as mh')
                  ->join('miner_info as mi','mi.mid','=','mh.mid')
                  ->join('miner_user as mu','mu.mid','=','mh.mid')
                  ->join('user as u','u.username','=','mu.user_name')
                  ->where('mh.mid',$mid)
                  ->select(
                      'mh.id',
                      'mh.hspace',
                      'mh.mid',
                      'mh.add_time',
                      'mi.m_number',
                      'mi.note',
                      'mu.user_name',
                      'mu.note as mu_note',
                      DB::raw('FROM_UNIXTIME(start_time,"%Y-%m-%d %H:%m:%s") as start_time'),
                      DB::raw('FROM_UNIXTIME(end_time,"%Y-%m-%d %H:%m:%s") as end_time'),
                      'mu.accessory',
                      'u.phone')
                  ->first();
              return s_msg('',$xq);

          }else{
              return e_msg(123456,'请传入矿机mid mid');
          }

    }

	
	 /**
     * @OA\Post(
     *     path="/minerEdit",
     *     tags={"矿机"},
     *     summary="修改附件和备注",
     *     @OA\Parameter(
     *         name="Authorization",
     *         in="header",
     *         description="bearer+token"
     *     ),
     *    @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 required={"mid"},
     *                 @OA\Property(property="mid", type="string", description="矿机mid,mid"),
     *                 @OA\Property(property="note", type="string", description="备注 note"),
     *                 @OA\Property(property="accessory", type="string", description="附件 accessory"),
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="正常",
     *     )
     * )
     */
    public function edit(Request $request)
    {

        $data = $request->all();
        return MinerLogic::edit($data);

    }
	
}
